package com.arx.item;

import com.arx.character.ARXCharacter;
import com.arx.character.Player;
import com.arx.interactive.Interactive;
import com.arx.interactive.InteractiveObject;
import com.arx.interactive.data.EquipItemElements;
import com.dalonedrau.util.GlobalConstants;

public class Equipment {
	/** the singleton instance. */
	private static Equipment	instance;
	/**
	 * Gets the one and only instance of {@link Equipment}.
	 * @return {@link Equipment}
	 */
	public static Equipment getInstance() {
		if (Equipment.instance == null) {
			Equipment.instance = new Equipment();
		}
		return Equipment.instance;
	}
	/** Hidden constructor. */
	private Equipment() {}
	// Sets an equipment property
	// -----------------------------------------------------------------------------------------------
	// ***********************************************************************************************
	public float ARX_EQUIPMENT_Apply(final ARXCharacter player,
			final InteractiveObject io, final byte ident, final float trueval) {
		if (io == null) {
			return trueval;
		}

		if (!io.equals(Interactive.getInstance().getInteractiveObject(0))) {
			return trueval;
		}
		float toadd = 0;

		for (byte i = 0; i < GlobalConstants.MAX_EQUIPED; i++) {
			if (player.getEquipped(i) != 0
					&& Interactive.getInstance().validIONum(
							player.getEquipped(i))) {
				InteractiveObject toequip = Interactive.getInstance()
						.getInteractiveObject(player.getEquipped(i));

				if (toequip != null
						&& toequip.hasIoFlag(Interactive.IO_ITEM)
						&& toequip.hasItemData()) {
					EquipItemElements elem =
							toequip.getItemData().getEquipItemElement(ident);

					if (elem != null
							&& elem.hasFlag(1)) {
						toadd += elem.getValue();
					}
				}
			}
		}
		return toadd;
	}
	public float ARX_EQUIPMENT_ApplyPercent(final ARXCharacter player,
			InteractiveObject io, byte ident, float trueval) {
		if (io == null) {
			return trueval;
		}

		if (!io.equals(Interactive.getInstance().getInteractiveObject(0))) {
			return trueval;
		}

		float toadd = 0;

		for (byte i = 0; i < GlobalConstants.MAX_EQUIPED; i++) {
			if (player.getEquipped(i) != 0
					&& Interactive.getInstance().validIONum(
							player.getEquipped(i))) {
				InteractiveObject toequip = Interactive.getInstance()
						.getInteractiveObject(player.getEquipped(i));

				if (toequip != null
						&& (toequip.getIoflags() & GlobalConstants.IO_ITEM) == GlobalConstants.IO_ITEM
						&& toequip.get_itemdata() != null
						&& toequip.get_itemdata().getEquipitem() != null) {
					EquipItemElements elem = toequip.get_itemdata()
							.getEquipItemElement(ident);
					if ((elem.getFlags() & 1) == 1) {
						toadd += elem.getValue();
					}
				}
			}
		}

		return toadd * trueval * GlobalConstants.DIV100;
	}
	public byte ARX_EQUIPMENT_GetPlayerWeaponType() {
		byte type = EquipmentGlobals.WEAPON_BARE;
		// the player is always Interactive Object 0
		InteractiveObject io =
				Interactive.getInstance().getInteractiveObject(0);

		if (io != null) {
			if (Player.getInstance().getPlayer()
					.getEquipped(EquipmentGlobals.EQUIP_SLOT_WEAPON) != 0
					&& Interactive
							.getInstance()
							.validIONum(
									Player.getInstance()
											.getPlayer()
											.getEquipped(
													EquipmentGlobals.EQUIP_SLOT_WEAPON))) {
				InteractiveObject toequip = Interactive.getInstance()
						.getInteractiveObject(
								Player.getInstance().getPlayer().getEquipped(
										EquipmentGlobals.EQUIP_SLOT_WEAPON));

				if (toequip != null) {
					if (toequip
							.hasTypeFlag(EquipmentGlobals.OBJECT_TYPE_DAGGER)) {
						type = EquipmentGlobals.WEAPON_DAGGER;
					}
					if (toequip.hasTypeFlag(EquipmentGlobals.OBJECT_TYPE_1H)) {
						type = EquipmentGlobals.WEAPON_1H;
					}
					if (toequip.hasTypeFlag(EquipmentGlobals.OBJECT_TYPE_2H)) {
						type = EquipmentGlobals.WEAPON_2H;
					}
					if (toequip.hasTypeFlag(EquipmentGlobals.OBJECT_TYPE_BOW)) {
						type = EquipmentGlobals.WEAPON_BOW;
					}
				}
			}
		}
		return type;
	}
	public void ARX_EQUIPMENT_IdentifyAll() {
		// the player is always Interactive Object 0
		InteractiveObject io =
				Interactive.getInstance().getInteractiveObject(0);

		if (io == null) {
			return;
		}

		if (!io.equals(Interactive.getInstance().getInteractiveObject(0))) {
			return;
		}
		if (io != null
				&& io.equals(Interactive.getInstance().getInteractiveObject(0))) {
			for (byte i = 0; i < GlobalConstants.MAX_EQUIPED; i++) {
				if (Player.getInstance().getPlayer().getEquipped(i) != 0
						&& Interactive.getInstance()
								.validIONum(
										Player.getInstance().getPlayer()
												.getEquipped(i))) {
					InteractiveObject toequip = Interactive.getInstance()
							.getInteractiveObject(
									Player.getInstance().getPlayer()
											.getEquipped(i));

					if (toequip != null
							&& (toequip.getIoflags() & GlobalConstants.IO_ITEM) == GlobalConstants.IO_ITEM
							&& toequip.get_itemdata() != null && toequip
									.get_itemdata().getEquipitem() != null) {
						float f0 = player.getFull_Skill_Object_Knowledge()
								+ player.getFull_Attribute_Mind();
						float f1 = toequip
								.get_itemdata()
								.getEquipItemElement(
										GlobalConstants.IO_EQUIPITEM_ELEMENT_Identify_Value)
								.getValue();
						if (f0 >= f1) {
							ARXScript.getInstance().SendIOScriptEvent(toequip,
									ARXScript.SM_IDENTIFY, "");
						}
					}
				}
			}
		}
	}
}
